package com.work.music.dao;

import com.work.music.entity.MusicLyrics;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

@Repository
public interface MusicLyricsMapper {
    @Delete({
        "delete from music_lyrics",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into music_lyrics (uid, songster_id, ",
        "type, album_id, name, ",
        "cover, addtime, ",
        "addip, status, reason, ",
        "synopsis, translate)",
        "values (#{uid,jdbcType=BIGINT}, #{songsterId,jdbcType=BIGINT}, ",
        "#{type,jdbcType=INTEGER}, #{albumId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, ",
        "#{cover,jdbcType=VARCHAR}, #{addtime,jdbcType=TIMESTAMP}, ",
        "#{addip,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{reason,jdbcType=VARCHAR}, ",
        "#{synopsis,jdbcType=LONGVARCHAR}, #{translate,jdbcType=LONGVARCHAR})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(MusicLyrics record);

    @Select({
        "select",
        "id, uid, songster_id, type, album_id, name, cover, addtime, addip, status, reason, ",
        "synopsis, translate",
        "from music_lyrics",
        "where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="uid", property="uid", jdbcType=JdbcType.BIGINT),
        @Result(column="songster_id", property="songsterId", jdbcType=JdbcType.BIGINT),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
        @Result(column="album_id", property="albumId", jdbcType=JdbcType.BIGINT),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="translate", property="translate", jdbcType=JdbcType.LONGVARCHAR)
    })
    MusicLyrics selectByPrimaryKey(Long id);

    @Select({
        "select",
        "id, uid, songster_id, type, album_id, name, cover, addtime, addip, status, reason, ",
        "synopsis, translate",
        "from music_lyrics"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="uid", property="uid", jdbcType=JdbcType.BIGINT),
        @Result(column="songster_id", property="songsterId", jdbcType=JdbcType.BIGINT),
        @Result(column="type", property="type", jdbcType=JdbcType.INTEGER),
        @Result(column="album_id", property="albumId", jdbcType=JdbcType.BIGINT),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="cover", property="cover", jdbcType=JdbcType.VARCHAR),
        @Result(column="addtime", property="addtime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="addip", property="addip", jdbcType=JdbcType.VARCHAR),
        @Result(column="status", property="status", jdbcType=JdbcType.INTEGER),
        @Result(column="reason", property="reason", jdbcType=JdbcType.VARCHAR),
        @Result(column="synopsis", property="synopsis", jdbcType=JdbcType.LONGVARCHAR),
        @Result(column="translate", property="translate", jdbcType=JdbcType.LONGVARCHAR)
    })
    List<MusicLyrics> selectAll();

    @Update({
        "update music_lyrics",
        "set uid = #{uid,jdbcType=BIGINT},",
          "songster_id = #{songsterId,jdbcType=BIGINT},",
          "type = #{type,jdbcType=INTEGER},",
          "album_id = #{albumId,jdbcType=BIGINT},",
          "name = #{name,jdbcType=VARCHAR},",
          "cover = #{cover,jdbcType=VARCHAR},",
          "addtime = #{addtime,jdbcType=TIMESTAMP},",
          "addip = #{addip,jdbcType=VARCHAR},",
          "status = #{status,jdbcType=INTEGER},",
          "reason = #{reason,jdbcType=VARCHAR},",
          "synopsis = #{synopsis,jdbcType=LONGVARCHAR},",
          "translate = #{translate,jdbcType=LONGVARCHAR}",
        "where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(MusicLyrics record);

    /**
     * 查询获取列表信息
     * @param start
     * @param count
     * @return
     */
    @Select("select a.* from (" +
            "select " +
            "l.id,l.name lname,l.cover lcover,l.type,a.name aname,s.name sname,a.cover acover,2 musicType,l.addtime,lstatus " +
            "from " +
            "music_lyrics l " +
            "left join music_album a on a.id=l.album_id " +
            "left join music_songster s on s.id=l.songster_id " +
            "${msg} " +
            "union " +
            "select " +
            "l.id,l.name lname,l.headimg lcover,0 type,'' aname,'' sname, '' acover, 0 musicType,l.addtime,lstatus " +
            "from " +
            "music_songster l " +
            "${msg} " +
            "union " +
            "select " +
            "l.id,l.name lname,l.cover lcover,0 type,'' aname,s.name sname, '' acover, 1 musicType,l.addtime,lstatus " +
            "from " +
            "music_album l " +
            "left join music_songster s on s.id=l.songster_id " +
            "${msg} " +
            " )a " +
            "order by a.addtime desc limit #{start},#{count}")
    List<Map<String,Object>> getSearchInfo(@Param("msg") String msg, @Param("start") int start, @Param("count") int count);
}